/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.m3d.texture.v2;

import java.nio.ByteBuffer;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Meanz
 */
public class Texture {

    private int id;
    private int width;
    private int height;
    private ByteBuffer imageData;

    public static Texture create() {
        return new Texture();
    }

    public Texture() {
    }

    public void build(int srcPixelFormat, int dstPixelFormat) {
        /*int srcPixelFormat = 0;
        int dstPixelFormat = GL_RGBA;
        if (image.getColorModel().hasAlpha()) {
        srcPixelFormat = GL_RGBA;
        } else {
        srcPixelFormat = GL_RGB;
        }*/

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        glTexImage2D(GL_TEXTURE_2D,
                0,
                dstPixelFormat,
                get2Fold(width),
                get2Fold(height),
                0,
                srcPixelFormat,
                GL_UNSIGNED_BYTE,
                imageData);
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public ByteBuffer getImageData() {
        return imageData;
    }

    public void setImageData(ByteBuffer imageData) {
        this.imageData = imageData;
    }

    /**/
    public void bind() {
        if (id == 0) {
            return;
        }
        glBindTexture(GL_TEXTURE_2D, id);
    }

    public int get2Fold(int fold) {
        int ret = 2;
        while (ret < fold) {
            ret *= 2;
        }
        return ret;
    }
}
